import {Component} from '@angular/core';
import {AlertController, IonicPage, MenuController, NavController} from 'ionic-angular';

import {CustomerProvider} from "../../../providers/customerProvider";

import {HomePage} from "../home/home";
import {CustomerRegisterPage} from "../customer-register/customer-register";
import {RetrievePasswordPage} from "../../../pages/retrieve-password/retrieve-password";
import {EmployeeLoginPage} from "../../../employee/pages/employee-login/employee-login";


/**
 * Generated class for the CustomerLoginPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-customer-login',
  templateUrl: 'customer-login.html',
})
export class CustomerLoginPage {
  customerLoginId: string;
  customerPassword: string;

  constructor(public navCtrl: NavController,
              public auth: CustomerProvider,
              public menuCtrl:MenuController,
              public alert: AlertController) {
  }

  login() {
    if (this.customerLoginId == null || this.customerPassword == null || this.customerLoginId == '' || this.customerPassword == '') {
      this.alertMsg('Error', 'Username Or password can\'t be null!');
      return false;
    }
    this.auth.login(this.customerLoginId, this.customerPassword).subscribe(res => {
      if (res.success) {
        this.navCtrl.setRoot(HomePage);
        this.menuCtrl.enable(true,'customerMenuID');
        this.menuCtrl.enable(false,'employeeMenuID');
      }
      else
        this.alertMsg('error', res.message);
    });
    /*if(! this.auth.login(this.customerLoginId, this.customerPassword))
     {
     this.alertMsg('Error','Credentials invalid!');
     return false;
     }

     this.navCtrl.setRoot(HomePage);*/
  }

  private alertMsg(title: string, subTitle: string) {
    let alert = this.alert.create({
      title: title,
      subTitle: subTitle,
      buttons: ['OK']
    });
    alert.present();
  }

  register() {
    this.navCtrl.setRoot(CustomerRegisterPage);
  }

  retireve() {
    this.navCtrl.setRoot(RetrievePasswordPage);
  }

  employee() {
    this.navCtrl.setRoot(EmployeeLoginPage);
  }
}
